2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_DTS_LICENSE_HEADER_START@
6 * IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
7 * ("Apple") in consideration of your agreement to the following terms, and your
8 * use, installation, modification or redistribution of this Apple software
9 * constitutes acceptance of these terms. If you do not agree with these terms,
10 * please do not use, install, modify or redistribute this Apple software.
12 * In consideration of your agreement to abide by the following terms, and
13 * subject to these terms, Apple grants you a personal, non-exclusive license,
14 * under Apple's copyrights in this original Apple software (the "Apple Software"),
15 * to use, reproduce, modify and redistribute the Apple Software, with or without
16 * modifications, in source and/or binary forms; provided that if you redistribute
17 * the Apple Software in its entirety and without modifications, you must retain
18 * this notice and the following text and disclaimers in all such redistributions
19 * of the Apple Software. Neither the name, trademarks, service marks or logos of
20 * Apple Computer, Inc. may be used to endorse or promote products derived from
21 * the Apple Software without specific prior written permission from Apple. Except
22 * as expressly stated in this notice, no other rights or licenses, express or
23 * implied, are granted by Apple herein, including but not limited to any patent
24 * rights that may be infringed by your derivative works or by other works in
25 * which the Apple Software may be incorporated.
27 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
28 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
29 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
31 * COMBINATION WITH YOUR PRODUCTS.
33 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
35 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
37 * DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
38 * CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
39 * APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * @APPLE_DTS_LICENSE_HEADER_END@
47 #include <sys/errno.h>
49 #include <dispatch/dispatch.h>
50 #include <mach/mach_time.h>
54 uint64_t elapsed_time
;
57 elapsed_time
= mach_absolute_time();
60 double timer_milePost() {
61 static dispatch_once_t justOnce
;
64 dispatch_once(&justOnce
, ^{
65 mach_timebase_info_data_t tbi
;
66 mach_timebase_info(&tbi
);
68 scale
= scale
/tbi
.denom
;
69 printf("Scale is %10.4f Just computed once courtesy of dispatch_once()\n", scale
);
72 uint64_t now
= mach_absolute_time()-elapsed_time
;
74 fTotalT
= fTotalT
* scale
; // convert this to nanoseconds...
75 fTotalT
= fTotalT
/ 1000000000.0;
82 dispatch_queue_t myQueue
= dispatch_queue_create("myQueue", NULL
);
83 dispatch_group_t myGroup
= dispatch_group_create();
85 // dispatch_apply on a serial queue finishes each block in order so the following code will take a little more than a second
87 dispatch_apply(kIT
, myQueue
, ^(size_t current
){
88 printf("Block #%ld of %d is being run\n",
89 current
+1, // adjusting the zero based current iteration we get passed in
91 usleep(USEC_PER_SEC
/10);
93 printf("and dispatch_apply( serial queue ) returned after %10.4lf seconds\n",timer_milePost());
95 // dispatch_apply on a concurrent queue returns after all blocks are finished, however it can execute them concurrently with each other
96 // so this will take quite a bit less time
98 dispatch_apply(kIT
, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0), ^(size_t current
){
99 printf("Block #%ld of %d is being run\n",current
+1, kIT
);
100 usleep(USEC_PER_SEC
/10);
102 printf("and dispatch_apply( concurrent queue) returned after %10.4lf seconds\n",timer_milePost());
104 // To execute all blocks in a dispatch_apply asynchonously, you will need to perform the dispatch_apply
105 // asynchonously, like this (NOTE the nested dispatch_apply inside of the async block.)
106 // Also note the use of the dispatch_group so that we can ultimatly know when the work is
110 dispatch_group_async(myGroup
, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0), ^{
111 dispatch_apply(kIT
, myQueue
, ^(size_t current
){
112 printf("Block #%ld of %d is being run\n",current
+1, kIT
);
113 usleep(USEC_PER_SEC
/10);
117 printf("and dispatch_group_async( dispatch_apply( )) returned after %10.4lf seconds\n",timer_milePost());
118 printf("Now to wait for the dispatch group to finish...\n");
119 dispatch_group_wait(myGroup
, UINT64_MAX
);
120 printf("and we are done with dispatch_group_async( dispatch_apply( )) after %10.4lf seconds\n",timer_milePost());